<?
class dbdURI
{
	private static $instance = null;
	private $controller = null;
	private $action = null;
	private $params = array();

	public static function getInstance()
	{
		if (self::$instance === null)
			self::$instance = new self();
		return self::$instance;
	}

	public static function set($uri)
	{
		$that = self::getInstance();
		$parts = preg_split("/\//", $uri, null, PREG_SPLIT_NO_EMPTY);
		$that->controller = array_shift($parts);
		$that->action = array_shift($parts);
		for ($i = 0; $i < count($parts); $i += 2)
			$that->params[$parts[$i]] = $parts[$i + 1];
	}

	public static function getController()
	{
		return self::getInstance()->controller;
	}

	public static function getAction()
	{
		return self::getInstance()->action;
	}

	public static function getParams()
	{
		return self::getInstance()->params;
	}

	public static function create($controller = null, $action = null, $args = array())
	{
		$uri = "/";
		if ($controller)
		{
			$uri .= $controller."/";
			if ($action)
				$uri .= $action;
			$uri .= "/";
			foreach ($args as $k => $v)
			{
				if ($v === "")
					continue;
				$uri .= $k."/".$v."/";
			}
		}
		return $uri;
	}

	public static function replace($baseUri = "", $controller = "", $action = "", $args = array())
	{
		if ($baseUri !== "")
			self::set($baseUri);
		if ($controller == "")
			$controller = self::getController();
		if ($action == "")
			$action = self::getAction();
		$tmp = self::getParams();
		foreach ($args as $k => $v)
			$tmp[$k] = $v;
		return self::create($controller, $action, $tmp);
	}
}
?>
